﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MySql.Data.MySqlClient;

public partial class setAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void bt_reset_Click(object sender, EventArgs e)
    {
        String user = Session["username"].ToString();
        String nowpw = now_pw.Text;
        String afpw = af_pw.Text;
        String afpw2 = af_pw2.Text;
        
        String connString = "server=localhost;database=test;uid=root;pwd=123456";
        MySqlConnection conn = new MySqlConnection(connString);
        conn.Open();
        String sqlselect = "select * from test.webuser where username = '" + user + "';";
        MySqlCommand mycommand = new MySqlCommand(sqlselect, conn);
        MySqlDataReader mdr = mycommand.ExecuteReader();
        if (mdr.Read())
        {
        if (nowpw != mdr.GetString("password").ToString())
        {
            Response.Write("<script>alert('密码错误')</script>");
            conn.Close();
        }
        else if(afpw == "")
        {
            Response.Write("<script>alert('修改的密码不能为空')</script>");
        }
        else if(afpw != afpw2)
        {
            Response.Write("<script>alert('两次输入的密码不一致！')</script>");
        }
        else if(afpw == afpw2)
        {

                try
                {
                conn.Close();
                conn.Open();
                String sql = "update test.webuser set password='"+afpw+ "' where username = '" + user + "';";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                Response.Write("<script>alert('更改成功');window.location='http://localhost:50015/Login.aspx';</script>");
                }
                catch
                {
                conn.Close();
                Response.Write("<script>alert('更改失败!!!!，联系管理员修复')</script>");
                }
        }
        }
        else
        {
            Response.Write("<script>alert('更改失败，联系管理员修复')</script>");
        }
    }

    protected void bt_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://localhost:50015/index.aspx", true);
    }

    protected void downit_Click(object sender, EventArgs e)
    {
        String user = Session["username"].ToString();
        String nowpw = now_pw.Text;
        String connString = "server=localhost;database=test;uid=root;pwd=123456";
        MySqlConnection conn = new MySqlConnection(connString);
        conn.Open();
        String sqlselect = "select * from test.webuser where username = '" + user + "';";
        MySqlCommand mycommand = new MySqlCommand(sqlselect, conn);
        MySqlDataReader mdr = mycommand.ExecuteReader();
        if (mdr.Read())
        {
            if (nowpw != mdr.GetString("password").ToString())
            {
                Response.Write("<script>alert('密码错误')</script>");
                conn.Close();
            }
            else
            {
                try
                {
                conn.Close();
                conn.Open();
                String sql = "delete from test.webuser where username = '" + user + "';";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                Response.Write("<script>alert('注销成功');window.location='http://localhost:50015/Login.aspx';</script>");
                }
               catch
                {
                conn.Close();
                Response.Write("<script>alert('注销失败!!!!，联系管理员修复')</script>");
                }
            }
        }
        else
        {
            Response.Write("<script>alert('注销失败，联系管理员修复')</script>");
        }
    }
}